/*
 * Copyright (c) 2013-2018 上海汇数数据. All rights reserved.
 * @(#) NumericBooleanSerializer.java 2018-08-05 22:18
 */

package cn.jh.common.core.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;


/**
 * true/false to 1/0.
 *
 * @author Fuchun
 * @since 1.0
 */
public class NumericBooleanSerializer extends JsonSerializer<Boolean> {

    @Override
    public void serialize(Boolean value, JsonGenerator gen, SerializerProvider serializers)
            throws IOException, JsonProcessingException {
        gen.writeNumber(value == null || !value ? 0 : 1);
    }
}
